package com.oyjd.driver;

import java.io.File;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.List;

import com.oyjd.FileOP;
import com.oyjd.HttpNet;
import com.oyjd.RegexUtil;

public class Down1 {
	public static String outDirverPath = "/sl/driver/";
	public static String outSqlPath = "/sl/down/";

	public static String base = "http://www.pc6.com";
	public static String url1 = base + "/qd/shoujiqudong_433_{0}.html";
	public static String reg_list = "<a href=\"(.*?)\" class=\"downBtn\">安全下载</a>";

	public static String reg_title = "<h1>(.*?)</h1>";
	public static String reg_plat = "<li class=\"fullsize\">应用平台：(.*?)</li>";
	public static String reg_down_urls = "<ul class=\"ul_Address\"><script type=\"text/javascript\" src=\"(.*?)\"></script></ul>";

	public static String reg_down = "<a href='(.*?)' target='_blank'";

	public static String sql = "insert into T_ADB_DRIVER (DRIVER_NAME,BUSINESS_ID,DEVICE_ID,FIT_BIT,OP_SYS,NOTE,SOU_PATH,ID,CREATOR,CREATE_TIME,LAST_EDITOR,LAST_UPDATE_TIME,STATUS) values (''{0}'',''{1}'',''{2}'',{3},{4},''{5}'',''{6}'',''{7}'',''admin'',now(),''admin'',now(),0);";

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		for (int i = 1; i <= 54; i++) {
			byte[] b = new HttpNet().getBytesFromNet(MessageFormat.format(url1, i));
			String c = new String(b, "gb2312");
			List<String> tmpList = RegexUtil.getMuti(c, reg_list);
			for (String s : tmpList) {
				doOne(s);
				Thread.sleep(2000L);
			}
		}
		System.out.println("完成");
	}

	/**
	 * 解析一个驱动信息
	 * 
	 * @param u
	 * @throws Exception
	 */
	public static void doOne(String u) throws Exception {
		try {
			HttpNet hn = new HttpNet();
			String c = new String(hn.getBytesFromNet(base + u), "gb2312");
			String urlList = RegexUtil.getSingle(c, reg_down_urls);
			String title = RegexUtil.getSingle(c, reg_title);
			String plat = RegexUtil.getSingle(c, reg_plat);

			String x = Dconst.getId(title);
			List<Integer> ops = Dconst.getOp(plat);
			if (null != x && ops.size() > 0) {
				String c1 = new String(hn.getBytesFromNet(base + urlList), "gb2312");
				List<String> tmpList = RegexUtil.getMuti(c1, reg_down);
				String fileName = downOne(tmpList);
				if (null != fileName) {
					for (int op : ops) {
						String fsql = MessageFormat.format(sql, title, x, "", 32, op, title, fileName, FileOP.getUUID());
						FileOP.writeFile(outSqlPath + "driverSql.txt", fsql + "\r\n", "gb2312", true);
					}
				}
			}
		} catch (Exception err) {
			err.printStackTrace();
			System.out.println(err.getMessage());
		}

	}

	/**
	 * 下载驱动文件
	 * 
	 * @param tmpList
	 * @return
	 * @throws Exception
	 */
	public static String downOne(List<String> tmpList) throws Exception {
		String flag = null;
		HttpNet hn = new HttpNet();
		Collections.sort(tmpList);
		for (String s : tmpList) {
			if (s.startsWith("http://") && s.endsWith("zip")) {
				String name = s.substring(s.lastIndexOf("/") + 1);
				if (!new File(outDirverPath + name).exists()) {
					if (hn.downFile(s, null, outDirverPath + name)) {
						flag = name;
						break;
					} else {
						new File(outDirverPath + name).delete();
					}
				} else {
					flag = name;
					break;
				}
			}
		}
		return flag;
	}

}
